<?php


namespace Ctrl;

use Lib;
use Lib\Paas\Login;
use Model\EquipModel;
use Model\HeroModel;
class Equip extends Lib\Base\Ctrl
{

    private $db;
    private $version;
    private $season;
    private $where;
    private $equipModel;
    public function __construct()
    {
        //校验管理员权限
        $roleService = new Lib\Paas\Role();
        $checkAdmin = $roleService->getRoleInfo();
//        if ($checkAdmin['role'] != ROLE_ID) {
//            die('抱歉！您没有访问权限');
//        }
//        if (!in_array(MODULE_ID, $checkAdmin['moduleID']) ) {
//            die('抱歉！您没有访问权限');
//        }
        $this->db = Lib\Db\DbModel::init();

        //获取当前版本
        $this->version = $this->db->table('tbversion')->where(['isCurrent'=>1])->find('id');
        $this->season = $this->db->table('tbseason')->where(['isCurrent'=>1])->find('id');
        /*if (empty($this->version) || empty($this->season)){
            header("Location: ". U('main/main'));
        }*/
        $this->where = [
            'versionId' =>$this->version['id'],
            'seasonId' =>$this->season['id'],
            'isDelete'=>0
        ];
        $this->assign('iActId','1001');
        $this->equipModel = new EquipModel();
    }



    public function index()
    {
        $keywords = I('keyword');
        $keywordsType = I('keywordsType',1);
        if ($keywords && !empty($keywords)) {
            $this->where['name'] = ['like','%'.$keywords.'%'];
        }

        $baseEquip = $this->equipModel->getAllEquipBase($this->where);

        //选择的英雄
//        $heroModel = new HeroModel();
//        $heros = $heroModel->getFormatHero($this->where);
//        $this->assign('selectHeros',json_encode($heros));

        $selectEquips = [];

        foreach ($baseEquip as &$value) {
            $selectEquips[$value['equipId']] = $value['name'];
            /*$value['heros'] = [];
            if($value['heroIds']) {
                $heroIds = explode(',',$value['heroIds']);
                foreach ($heroIds as $heroId) {
                    $value['heros'][] = [
                        'key'=>$heroId,
                        'name'=>$heros[$heroId],
                    ];
                }
            }*/


        }
        $this->assign('selectEquips',json_encode($selectEquips));


        //获取所有基础道具
        $this->assign('baseEquip',$baseEquip);

        $this->assign('keywords',$keywords);
        $this->assign('keywordsType',$keywordsType);

        $this->display('Admin/Equip/index');
    }



    //根据基础道具获取对应的合成道具
    public function getEquipByBase()
    {
        $equipId = I('equipId');
        $keywords = I('keyword');
        if ($keywords && !empty($keywords)) {
            $this->where['name'] = $keywords;
        }
        if ($equipId) {
            $data = $this->equipModel->getEquipByBase($equipId,$this->where);
        } else {
            $data = $this->equipModel->getAllEquip($this->where);
        }



        $this->assign('data',$data);
        $this->display('Admin/Equip/type2');
    }


    public function update()
    {

        $post = I('post.');

        $equipId = $post['equipId'];
        $type = $post['type'];
        $TFTID = I('TFTID');
        if (!is_numeric($TFTID)) {
            $this->ajaxError('TFTID必须为数字类型');
        }
        if (!is_numeric($equipId)) {
            $this->ajaxError('道具ID必须为数字类型');
        }
        if (empty($post['name'])) {
            $this->ajaxError('道具名称不能为空');
        }
        if (empty($post['effect'])) {
            $this->ajaxError('效果不能为空');
        }
        if (empty($post['keywords'])) {
            $this->ajaxError('关键词不能为空');
        }
        if (empty($post['imagePath'])) {
            $this->ajaxError('请上传图标');
        }
//检查当前赛季和版本TFTID是否重复
        if($this->equipModel->checkTFTID($equipId,$this->where,$TFTID)){
            $this->ajaxError('该TFTID已存在');
        }

        $user = new Lib\Paas\Login();
        $userName = $user->getLoginInfo()['name'];

        $data = [
            'TFTID'=>$TFTID,
            'name'=>$post['name'],
            'effect'=>$post['effect'],
            'keywords'=>$post['keywords'],
        //    'heroIds'=>$post['heroIds'],
            'imagePath'=>$post['imagePath'],
            'updateMan'=>$userName,
            'updateTime'=>date('Y-m-d H:i:s'),
        ];
        if($type==2){
            if (empty($post['formula'])) {
                $this->ajaxError('请选择合成道具');
            }
            $data['formula'] = $post['formula'];
        }



        $res = $this->equipModel->updateEquip($equipId,$this->where,$data);
        logWrite(parent::LOG_TYPE_EDIT,$res,'tbequip');
        if($res!==false){
            $this->ajaxSuccess("编辑成功");
        } else {

            $this->ajaxError('编辑失败');
        }
    }

    public function add()
    {
        $baseEquip = $this->equipModel->getAllEquipBase($this->where);
        $selectEquips = [];
        foreach ($baseEquip as &$value) {
            $selectEquips[$value['equipId']] = $value['name'];
        }
        $this->assign('selectEquips',json_encode($selectEquips));

        $this->display('Admin/Equip/add');
    }

    public function addSave()
    {
        $post = I('post.');
        $type = $post['type'];
        $TFTID = I('TFTID');
        if (!is_numeric($TFTID)) {
            $this->ajaxError('TFTID必须为数字类型');
        }

        if (!is_numeric($post['equipId'])) {
            $this->ajaxError('道具ID必须为数字类型');
        }
        if (empty($post['name'])) {
            $this->ajaxError('道具名称不能为空');
        }
        if (empty($post['effect'])) {
            $this->ajaxError('效果不能为空');
        }
        if (empty($post['keywords'])) {
            $this->ajaxError('关键词不能为空');
        }
        if (empty($post['imagePath'])) {
            $this->ajaxError('请上传图标');
        }

        //检查当前赛季和版本TFTID是否重复
        if($this->equipModel->checkTFTID($post['equipId'],$this->where,$TFTID)){
            $this->ajaxError('该TFTID已存在');
        }

        $user = new Lib\Paas\Login();
        $userName = $user->getLoginInfo()['name'];
        $data = [
            'TFTID'=>$TFTID,
            'equipId'=>$post['equipId'],
            'name'=>$post['name'],
            'effect'=>$post['effect'],
            'keywords'=>$post['keywords'],
            'type' => $type,
            //    'heroIds'=>$post['heroIds'],
            'imagePath'=>$post['imagePath'],
            'creater'=>$userName,
            'createTime'=>date('Y-m-d H:i:s'),
        ];
        if($type==2){
            if (empty($post['formula'])) {
                $this->ajaxError('请选择合成道具');
            }
            $data['formula'] = $post['formula'];
        }
        $where = [
            'versionId' => $this->version['id'],
            'seasonId' => $this->season['id'],
            'isDelete' => 1
        ];
        if ($this->equipModel->getEquipInfo($post['equipId'],$where)) {
            $this->ajaxError('道具已存在');
        }

        $res = $this->equipModel->insertRaceOne($this->version['id'],$this->season['id'],$data);
        logWrite(parent::LOG_TYPE_INSENT, $res, 'tbequip');
        if($res!==false){
            $this->ajaxSuccess("添加成功");
        } else {

            $this->ajaxError('添加失败');
        }

    }




}